package com.ruoyi.fn.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fn.domain.FinCategory;
import com.ruoyi.fn.service.IFinCategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ShiroUtils;
/**
 * 收支分类Controller
 * 
 * @author ruoyi
 * @date 2025-05-26
 */
@Controller
@RequestMapping("/fn/category")
public class FinCategoryController extends BaseController
{
    private String prefix = "fn/category";

    @Autowired
    private IFinCategoryService finCategoryService;

    @RequiresPermissions("fn:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询收支分类列表
     */
    @RequiresPermissions("fn:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FinCategory finCategory)
    {
        startPage();
        List<FinCategory> list = finCategoryService.selectFinCategoryList(finCategory);
        return getDataTable(list);
    }

    /**
     * 导出收支分类列表
     */
    @RequiresPermissions("fn:category:export")
    @Log(title = "收支分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FinCategory finCategory)
    {
        List<FinCategory> list = finCategoryService.selectFinCategoryList(finCategory);
        ExcelUtil<FinCategory> util = new ExcelUtil<FinCategory>(FinCategory.class);
        return util.exportExcel(list, "收支分类数据");
    }

    /**
     * 新增收支分类
     */
    @RequiresPermissions("fn:category:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("userString", ShiroUtils.getSysUser().toUserString());
        return prefix + "/add";
    }

    /**
     * 新增保存收支分类
     */
    @RequiresPermissions("fn:category:add")
    @Log(title = "收支分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FinCategory finCategory)
    {
        finCategory.setUserId(ShiroUtils.getUserId());
        return toAjax(finCategoryService.insertFinCategory(finCategory));
    }

    /**
     * 修改收支分类
     */
    @RequiresPermissions("fn:category:edit")
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Long categoryId, ModelMap mmap)
    {
        FinCategory finCategory = finCategoryService.selectFinCategoryByCategoryId(categoryId);
        finCategory.setUserId(ShiroUtils.getUserId());
        mmap.put("finCategory", finCategory);
        mmap.put("userString", ShiroUtils.getSysUser().toUserString());
        return prefix + "/edit";
    }

    /**
     * 修改保存收支分类
     */
    @RequiresPermissions("fn:category:edit")
    @Log(title = "收支分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FinCategory finCategory)
    {
        return toAjax(finCategoryService.updateFinCategory(finCategory));
    }

    /**
     * 删除收支分类
     */
    @RequiresPermissions("fn:category:remove")
    @Log(title = "收支分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(finCategoryService.deleteFinCategoryByCategoryIds(ids));
    }
}
